package com.newtouch.xcd.common.validator.impl;

import cn.hutool.core.lang.Validator;
import com.newtouch.xcd.common.validator.IsPhone;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;

public class IsPhoneValidator implements ConstraintValidator<IsPhone, String> {

    private boolean required;

    // 重写initialize方法获取注解实例
    @Override
    public void initialize(IsPhone ca) {
        // 重注解实例中获信息
        required = ca.required();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        // value就是要校验的数据了
        if (value != null && required) {
            return Validator.isMobile(value);
        }
        return false;
    }
}
